import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  // 为了持久化存储状态，借助本地存储
  let userMsgLocal = localStorage.getItem("userMsg");
  userMsgLocal = userMsgLocal ? JSON.parse(userMsgLocal) : {};

  // 创建响应式状态
  const userMsg = ref( userMsgLocal );

  // userMsg对象可能包含的字段：{token, uName, uId, power, avatar}
  function change_userMsg(obj){
    if(obj === null){
      userMsg.value = {};
    }else{
      obj.token && (userMsg.value.token = obj.token);
      obj.uName && (userMsg.value.uName = obj.uName);
      obj.uId && (userMsg.value.uId = obj.uId);
      obj.power && (userMsg.value.power = obj.power);
      obj.avatar && (userMsg.value.avatar = obj.avatar);
    }
  }

  // 暴露响应式状态和方法
  return { userMsg, change_userMsg }
})


export const useTeamStore = defineStore('team', () => {
  let teamMsgLocal = localStorage.getItem("teamMsg");
  teamMsgLocal = teamMsgLocal ? JSON.parse(teamMsgLocal) : {};

  const teamMsg = ref( teamMsgLocal );

  // teamMsg对象可能包含的字段：[{tId, tName},{tId, tName}]
  function change_teamMsg(obj){
    teamMsg.value = obj;
  }

  return { teamMsg, change_teamMsg }
})


